﻿using Collaboration_System.DataBase;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace Collaboration_System
{
    /// <summary>
    /// Логика взаимодействия для TaskWindow.xaml
    /// </summary>
    public partial class TaskWindow : Window
    {
        Collaboration_SystemEntities entity = new Collaboration_SystemEntities();
        public List<Notes> notes;
        public List<Users> users;
        public List<DataBase.Task> task;

        public TaskWindow()
        {
            InitializeComponent();

        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            RefreshTable();
            string idUserString = File.ReadLines("config.json").Skip(1).First();
            UserName.Text = idUserString;
            string idRoleString = File.ReadLines("config.json").Skip(2).First();
            Role.Text = idRoleString;
        }
        private void RefreshTable()
        {
            string idString = File.ReadLines("config.json").Skip(0).First();
            string idRoleString = File.ReadLines("config.json").Skip(2).First();
            Role.Text = idRoleString;
            int idInt = Int32.Parse(idString);
            DateTime curDate = DateTime.Now;
            if (idRoleString.Contains("Начальник"))
            {
                switch (TaskTabControl.SelectedIndex)
                {
                    case 0:
                        {
                          NewTaskDataGrid.ItemsSource = entity.Task.Where(t => t.status == "Новые").ToList();
                        }
                        break;
                    case 1:
                        { AtWorkDataGrid.ItemsSource = entity.Task.Where(t => t.status == "В работе").ToList(); }
                        break;
                    case 2:
                        { ForVerificationDataGrid.ItemsSource = entity.Task.Where(t => t.status == "На проверку").ToList(); }
                        break;
                    case 3:
                        { CompletedSuccessfullyDataGrid.ItemsSource = entity.Task.Where(t => t.status == "Завершенные успешно").ToList(); }
                        break;
                    case 4:
                        { UnfulfilledDataGrid.ItemsSource = entity.Task.Where(t => t.status == "Не выполненные").ToList(); }
                        break;

                }
            }
            else
            {

                switch (TaskTabControl.SelectedIndex)
                {
                    case 0:
                        { NewTaskDataGrid.ItemsSource = entity.Task.Where(t => t.user_id == idInt && t.status == "Новые").ToList(); }
                        break;
                    case 1:
                        { AtWorkDataGrid.ItemsSource = entity.Task.Where(t => t.user_id == idInt && t.status == "В работе").ToList(); }
                        break;
                    case 2:
                        { ForVerificationDataGrid.ItemsSource = entity.Task.Where(t => t.user_id == idInt && t.status == "На проверку").ToList(); }
                        break;
                    case 3:
                        { CompletedSuccessfullyDataGrid.ItemsSource = entity.Task.Where(t => t.user_id == idInt && t.status == "Завершенные успешно").ToList(); }
                        break;
                    case 4:
                        { UnfulfilledDataGrid.ItemsSource = entity.Task.Where(t => t.user_id == idInt && t.status == "Не выполненные").ToList(); }
                        break;

                }
            }
        }
        private void Task_Click(object sender, RoutedEventArgs e)
        {
            TaskWindow autorization = new TaskWindow();
            autorization.Show();
            Close();
        }
        private void AddTask_Click(object sender, RoutedEventArgs e)
        {
            AddNewTaskWindow addTask = new AddNewTaskWindow();
            addTask.Show();
        }
        private void Lids_Click(object sender, RoutedEventArgs e)
        {
            LidsWindow autorization = new LidsWindow();
            autorization.Show();
            Close();
        }

        private void Deal_Click(object sender, RoutedEventArgs e)
        {
            TransactionsWindow autorization = new TransactionsWindow();
            autorization.Show();
            Close();
        }

        private void Company_Click(object sender, RoutedEventArgs e)
        {
            CompanyWindow autorization = new CompanyWindow();
            autorization.Show();
            Close();
        }

        private void Product_Click(object sender, RoutedEventArgs e)
        {
            ProductWindow autorization = new ProductWindow();
            autorization.Show();
            Close();
        }

        private void ExitAkk_Click(object sender, RoutedEventArgs e)
        {
            File.Delete("config.json");
            Autorization autorization = new Autorization();
            autorization.Show();
            Close();
        }
        private void Home_Click(object sender, RoutedEventArgs e)
        {
            Dashboard autorization = new Dashboard();
            autorization.Show();
            Close();
        }

        private void ClouseButton_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Close();
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
           RefreshTable();
        }

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            RefreshTable();
        }

        private void NewDeleteButton_Click(object sender, RoutedEventArgs e)
        {
            var message = MessageBox.Show("Вы хотите перенести данные?", "Уведомление", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (message.Equals(MessageBoxResult.Yes))
            {
                var selected = NewTaskDataGrid.SelectedItem as DataBase.Task;
                if (selected != null)
                {
                    selected.status = "В работе";
                    entity.SaveChanges();
                    MessageBox.Show("Статус успешно сменен", "Уведомление", MessageBoxButton.OK, MessageBoxImage.Information);
                    RefreshTable();
                }
                else
                {
                    MessageBox.Show("Вы не выбрали строку для смены статуса!", "Уведомление", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
        }

        private void AtWorkNextButton_Click(object sender, RoutedEventArgs e)
        {
            var message = MessageBox.Show("Вы хотите перенести данные?", "Уведомление", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (message.Equals(MessageBoxResult.Yes))
            {
                var selected = AtWorkDataGrid.SelectedItem as DataBase.Task;
                if (selected != null)
                {
                    selected.status = "На проверку";
                    entity.SaveChanges();
                    MessageBox.Show("Статус успешно сменен", "Уведомление", MessageBoxButton.OK, MessageBoxImage.Information);
                    RefreshTable();
                }
                else
                {
                    MessageBox.Show("Вы не выбрали строку для смены статуса!", "Уведомление", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
        }

        private void ForVerificationButton_Click(object sender, RoutedEventArgs e)
        {
            var message = MessageBox.Show("Вы хотите перенести данные?", "Уведомление", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (message.Equals(MessageBoxResult.Yes))
            {
                var selected = ForVerificationDataGrid.SelectedItem as DataBase.Task;
                if (selected != null)
                {
                    selected.status = "Завершенные успешно";
                    entity.SaveChanges();
                    MessageBox.Show("Статус успешно сменен", "Уведомление", MessageBoxButton.OK, MessageBoxImage.Information);
                    RefreshTable();
                }
                else
                {
                    MessageBox.Show("Вы не выбрали строку для смены статуса!", "Уведомление", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
        }

        private void CompletedSuccessfullyButton_Click(object sender, RoutedEventArgs e)
        {

        }

        private void UnfulfilledButton_Click(object sender, RoutedEventArgs e)
        {
           
        }

        private void TabItem_MouseDown(object sender, MouseButtonEventArgs e)
        {
            RefreshTable();
        }

        private void TabItem_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            RefreshTable();
        }

        private void TabItem_MouseDown_2(object sender, MouseButtonEventArgs e)
        {
            RefreshTable();
        }

        private void TabItem_MouseDown_3(object sender, MouseButtonEventArgs e)
        {
            RefreshTable();
        }

        private void TabItem_MouseDown_4(object sender, MouseButtonEventArgs e)
        {
            RefreshTable();
        }

        private void TaskTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RefreshTable();
        }
    }
}
